<html>
<head>
<title>Scenario 6.2</title>
</head>

<body>
<h3>Scenario 6.2</h3>


<p>
User wants to process a series of XML examples one at a time.  For
each example they want to:

<pre>
       IF the example is valid
               i) Update the Discrimination definitions of Suite Foo
               ii) Apply the model currently implicit in the training state of Suite 
                   Foo to the example to make predictions, and THEN
               iii) Update training state of Suite Foo using this example
       ELSE
               i&#39;) Stop processing.  The invalid example should have no impact on Suite Foo.
</pre>


<p>
<strong>Implementation.</strong> 
This use case combines operations from scenarios 3.2 and 4.2.

<pre>
  ...
  // or you can just use stderr...
  Logger logger = Logger.getLogger("MyLog"); 

  Suite suite = new Suite("foo.xml"); // create suite...
  // Add any learners 
  ....
  // get the suite's learner (assuming there is just 1)
  Learner learner = suite.getAllLearners()[0];

  // modify suite by any preceding definitional parsing etc
  ....                                
 
  final boolean isDefinitional = true;
  // create a lightweight copy of the suite (so that any modifications
  // that may happen during validation will only affect this copy, and not
  // your "main" suite
  Suite validator = suite.lightweightCopyOf("Validator");

  int cnt = 0;
  boolean error = false;
  // Obtain all "datapoint" elements from somewhere
  org.w3c.dom.Element[] elements = ....
  for(Element pe: elements) {
      try {
         // validate the example
         DataPoint dummy=ParseXML.parseDataPoint(pe, validator, isDefinitional); 
         // If we got here, the validator had no problems, so the real suite 
         // should not either.

          // (i) Parse the example's XML against the "real" suite, updating the latter
         DataPoint p=ParseXML.parseDataPoint(pe, suite, isDefinitional); 
          // (ii) Score the example
          double[][] scores = learner.applyModel(p);
          // now, scores[i][j] contains the learner-estimated probability of
          // the example p's belonging to the j-th class of the i-th 
          // discrimination of the suite

          // (iii) Train the learner on the example
          learner.absorbExample(p);
          cnt ++; // keep going
       } catch (Exception e) {
          logger.error("Error when validating the " + (cnt+1) + "-th example: " + e);
           error = true;
           break; // (i') stop processing
       }
    }
 }

 if (!error) logger.info("All " + cnt + " examples validated successfully");
</pre>

<hr>

Back to <a href="standard-scenarios.html">All scenarios</a>

</body>
</html>
